package com.example.homecunter;

import android.Manifest;
import android.util.Log;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.common.utils.BaseEntity;
import com.example.common.utils.OnItemClickListener;
import com.example.common.utils.PermisssionUtils;
import com.example.homecunter.video.adapter.ChannelAdapter;
import com.example.homecunter.video.adapter.VideoAdapter;
import com.example.homecunter.databinding.ActivityHomeCenterBinding;
import com.example.homecunter.video.entity.ChannelEntity;
import com.example.homecunter.video.entity.VideoEntity;
import com.example.homecunter.video.viewmodel.ChannelViewModel;
import com.example.mvvmcore.view.BaseMVVMActivity;

import java.util.List;

@Route(path = "/homecenter/main")
public class HomeCenterActivity extends BaseMVVMActivity<ChannelViewModel> {
    ActivityHomeCenterBinding binding;
    ChannelAdapter channelAdapter;
    VideoAdapter videoAdapter;

    int channelid=1;
    int page=1;

//    public MutableLiveData<ChannelEntity> data = new MutableLiveData<>();
    @Override
    protected void initEnv() {
        super.initEnv();

        PermisssionUtils.getInstance().requestPermission(this, new PermisssionUtils.RequestPermissionCallback() {
            @Override
            public void onSuccess() {
                //所有权限申请成功

            }

            @Override
            public void onFailed() {
                //有权限未授权 提醒用户再次授权
            }
        }, Manifest.permission.INTERNET);
    }

    @Override
    protected Class getViewModelClazz() {
        return ChannelViewModel.class;
    }

    @Override
    protected void initData() {
        binding = DataBindingUtil.setContentView(this,R.layout.activity_home_center);
        binding.setOwn(this);

        mViewModel.channel().observe(this, new Observer<BaseEntity<List<ChannelEntity>>>() {
            @Override
            public void onChanged(BaseEntity<List<ChannelEntity>> listBaseEntity) {
                List<ChannelEntity> data1 = listBaseEntity.getData();

                channelAdapter = new ChannelAdapter(HomeCenterActivity.this,data1);

                binding.tabrv.setLayoutManager(new LinearLayoutManager(HomeCenterActivity.this, RecyclerView.HORIZONTAL, true));
                binding.tabrv.setAdapter(channelAdapter);


                channelAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        channelid = position+1;

                        mViewModel.video(channelid,page).observe(HomeCenterActivity.this, new Observer<BaseEntity<List<VideoEntity>>>() {
                            @Override
                            public void onChanged(BaseEntity<List<VideoEntity>> listBaseEntity) {
                                List<VideoEntity> videoData = listBaseEntity.getData();

                                videoAdapter = new VideoAdapter(HomeCenterActivity.this,videoData);

                                binding.videoRv.setLayoutManager(new LinearLayoutManager(HomeCenterActivity.this));
                                binding.videoRv.setAdapter(videoAdapter);


                            }
                        });

                        videoAdapter.notifyDataSetChanged();
                        Log.e("Main", "" + channelid + "被点击了！！");
                        Toast.makeText(HomeCenterActivity.this, "+" + channelid + "被点击了！！", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });


        mViewModel.video(channelid,page).observe(this, new Observer<BaseEntity<List<VideoEntity>>>() {
            @Override
            public void onChanged(BaseEntity<List<VideoEntity>> listBaseEntity) {
                List<VideoEntity> videoData = listBaseEntity.getData();

                videoAdapter = new VideoAdapter(HomeCenterActivity.this,videoData);

                binding.videoRv.setLayoutManager(new LinearLayoutManager(HomeCenterActivity.this));
                binding.videoRv.setAdapter(videoAdapter);


            }
        });

    }

//extends BaseActivity {
//
//    Handler mHandler = new Handler(Looper.myLooper());
//
//    @Autowired(name = "param1")
//    public String param1 = "";
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        ARouter.getInstance().inject(this);
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_home_center);
//
//        mHandler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//
//            }
//        }, 2000);
//    }
//
//    @Override
//    protected void initData() {
//        showMsg(String.format("接收到传递的参数是：%s", param1));
//
//    }


}